

*1)  ACS 2017-2021 - Mexican migrants sample 

use "${data}ACS2017-2021_finalready.dta", clear
	
	* mexican born men 
	keep if country_ori ==1
	keep if sex==1 

	g datasource= "ACS"

	*keep men coresiding with spouse 
	keep if marst==1 & spouse_marst==1  
 
	* drop married same year as migration 
	drop if  status_evermarried ==2
    *drop married beofre migration fpr migrants 
	drop if status_evermarried==1 
 
	*keep 18- 40  
	keep if inrange(age,18,40)
 
	*keep migrants aged 18-40 when migrated 
	keep if inrange(agemig,18,40) 
 
	
	save "${data}temp.dta",replace
	
	
	
*2) Mexican Census 2020 - non-migrant sample  
 
clear
use "${data}Mexcensus2020_final.dta" if  sexo==1  &  inrange(age,18,60) 

	

 label def maaa ///
		  1 "Married, spouse present" ///
           2 "Married, spouse absent" ///
           3 "Separated" ///
           4 "Divorced" ///
           5 "Widowed" ///
           6 "Never married/single", modify 
		   
	g       marst= 1 if inlist(situa_conyugal,1,5,6,7) & spouse_present==1 
	replace marst= 2 if inlist(situa_conyugal,1,5,6,7) & spouse_present==0 
	replace marst= 3 if situa_conyugal== 2
	replace marst= 4 if situa_conyugal== 3
	replace marst= 5 if situa_conyugal== 4
	replace marst= 6 if situa_conyugal== 8		   
	g       spouse_marst= 1 if inlist(spouse_situa_conyugal,1,5,6,7) & spouse_present==1 
	replace spouse_marst= 2 if inlist(spouse_situa_conyugal,1,5,6,7) & spouse_present==0 
	replace spouse_marst= 3 if spouse_situa_conyugal== 2
	replace spouse_marst= 4 if spouse_situa_conyugal== 3
	replace spouse_marst= 5 if spouse_situa_conyugal== 4
	replace spouse_marst= 6 if spouse_situa_conyugal== 8

	label values  marst  maaa		   
	label values spouse_marst maaa 

	g marr= inlist(marst,1,2)
	g single= marst== 6
	g birthyr= 2020-age 
	
	label var spouse_age "spouse: age"
	label var spouse_yedu "spouse: years education"
			
	g     spouse_orig_st = 1 if spouse_birthplace==1
	replace   spouse_orig_st= 2 if spouse_birthplace==3
	replace   spouse_orig_st= 3 if inlist(spouse_birthplace,2,4)

	
	*Keep Married with spouse present 
	keep if marst==1 & spouse_marst==1 
	
	* keep born in Mexico
	keep if inrange(ent_pais_nac,1,32) 
	keep if birthplace==1 
	
	*Age: 18-40 
	keep if inrange(age,18,40)
	 
	 * drop returnees 
	 drop if return_US==1 
 
	g datasource= "Mexcens2020"

*keep sex age edattaind yedu spouse_present spouse_age spouse_yedu spouse_edattaind  marst spouse_marst spouse_orig_st datasource 


 append using "${data}temp.dta" 
 erase "${data}temp.dta" 
 
 
 
 
 *TREATMENT
 g         married_inUS= 1 if status_evermarried==3 
 replace   married_inUS= 0 if datasource=="Mexcens2020" |  status_evermarried==1 
  
  label var married_inUS      "US mig: Married aft. mig"
  

		
*Outcome 
		
		label var spouse_age "spouse: age"
		label var spouse_yedu "spouse: years education"
		
		g spouse_USborn= spouse_orig_st==2 
		label var spouse_USborn "spouse: born in US"
		
		gen diff_yedu= spouse_yedu - yedu 
		gen spouse_moredu = diff_yedu>1
		label var spouse_moredu "spouse more educated"
		
		gen diff_age= spouse_age- age 
		gen spouse_older= diff_age>1
		label var spouse_older "spouse older"
		label var diff_age "spouse age difference"
		
		
		g spouse_lang_nospan= .
		replace spouse_lang_nospan=0 if  datasource=="Mexcens2020" 
		replace spouse_lang_nospan= 1 if  datasource!="Mexcens2020" 
		replace spouse_lang_nospan= 0 if spouse_language==12 

		label var spouse_lang_nospan "spouse language: not Spanish"
	
  
  keep spouse_age spouse_older  spouse_yedu  spouse_moredu   spouse_USborn spouse_lang_nospan  ///
       married_inUS birthyr yedu  sex age edattaind yedu spouse_present spouse_age spouse_yedu spouse_edattaind  birthyr marst spouse_marst spouse_orig_st status_evermarried  evermarried  evermarr_det agemig  yrsusa1 country_ori datasource  spouse_marst  


save "${data}ACS_Mexcensus_combined.dta", replace 				
	
	